package Threelayer.DAO.impl;

import Threelayer.DAO.PersonDao;
import Threelayer.Entity.Person;
import Threelayer.Utils.DaoUtils;
import Threelayer.Utils.DateUtils;
import Threelayer.Utils.PersonRowMapper;
import Threelayer.Utils.RowMapper;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lishunlin110
 * @version 1.0
 * @description: 人员dao 实现增删改查
 * @date 2021/8/23 12:46
 */
public class PersonDaoImpl implements PersonDao {
    @Override
    public int insert(Person person) {
        DaoUtils<Person> daoUtils = new DaoUtils<>();
        String sql="insert into person(name,dept_name,tot_cred,bry) values(?,?,?,?)";
        Object[] arg={person.getName(),person.getDept_name(),person.getTot_cred(), DateUtils.utilToSql(person.getBirthday())};
        return daoUtils.commonsUpdate(sql,arg);
    }

    @Override
    public int delete(int id) {
        DaoUtils<Person> daoUtils = new DaoUtils<>();
        String sql = "delete from person where id=?";
        Object[] arg={id};
        return daoUtils.commonsUpdate(sql,arg);
    }

    @Override
    public int update(Person person) {
        DaoUtils<Person> daoUtils = new DaoUtils<>();
        String sql="update person set name=?,dept_name=?,tot_cred=?,bry=? where id=?";
        //String sql="update student set name=?,dept_name=?,tot_cred=? where ID=?";
        Object[] arg={person.getName(),person.getDept_name(),person.getTot_cred(), DateUtils.utilToSql(person.getBirthday()),person.getId()};
        return daoUtils.commonsUpdate(sql,arg);
    }

    @Override
    public Person query(String name) {
        String sql = "select* from person where name=?";
        DaoUtils<Person> daoUtils = new DaoUtils<>();
        RowMapper<Person> rowMapper = new PersonRowMapper();
        List<Person> personList = daoUtils.commonsQuery(sql,rowMapper,name);
        if(personList!=null){
            return personList.get(0);
        }else {
            return null;
        }
    }

    @Override
    public List<Person> queryAll() {

        String sql = "select* from person";
        DaoUtils<Person> daoUtils = new DaoUtils<>();
        RowMapper<Person> rowMapper = new PersonRowMapper();
        return daoUtils.commonsQuery(sql,rowMapper,null);
    }
}
